package com.sykj.bsc.cl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.bsc.cl.Util.AccountInfo;
import com.sykj.bsc.cl.Util.IndustrySMS;
import com.sykj.bsc.cl.service.WechatCommonService;
import com.sykj.bsc.jhy.entity.Bis_bag_project;
import com.sykj.bsc.jhy.entity.Bis_nurse_bag;
import com.sykj.bsc.jhy.entity.Bis_nurse_project;
import com.sykj.bsc.jhy.service.serviceImpl.Bis_nurse_projectServiceImpl;
import com.sykj.bsc.wsy.service.Bis_beauty_configService;
import com.sykj.bsc.zjj.bean.Bis_custom_user;
import com.sykj.bsc.zjj.service.Bis_shop_userService;

/*
 * 微信核心页面跳转类
 */
@Controller
@RequestMapping("wechat")
public class LoginController {
	//王舒雅 wsy
	@Autowired
	Bis_beauty_configService service;
	//江海阳 jhy
	@Autowired 
	Bis_nurse_projectServiceImpl impl;
	//张俊杰 zjj
	@Autowired
	private Bis_shop_userService bsus;
	@Autowired
	private WechatCommonService commonService;
	
	//用来显示美颜社的登陆页面
	@RequestMapping("/login")
	public String login(){
		return "/backstage/cl/wechat/login";
	}

	//用来进行美颜社登陆的验证
	@RequestMapping("/vaildate")
	@ResponseBody
	public String vaildate(HttpServletRequest request,HttpSession session){
		Map<String, Object> map =init(request);
		Bis_custom_user list12 = bsus.getMobileBis_custom_user(map);
		session.setAttribute("user", list12);
		if (list12!=null && !list12.equals("")) {
			return "1";
		}else{
			return "0";
		}
	}
	
	//用来显示美颜社的注册页面
	@RequestMapping("/enroll")
	public String  enroll(){
		return "/backstage/cl/wechat/enroll";
	}
	//用来获取验证码
	@RequestMapping("/code")
	@ResponseBody
	public String code(@RequestParam("mobile")String mobile,HttpSession session){
		// 获取开发者账号信息
		AccountInfo.execute();
		//生成6位随机数
		int max=999999;
        int min=100000;
        Random random = new Random();
        Integer ran = random.nextInt(max)%(max-min+1) + min;
        // 验证码通知短信接口   手机号和发送短信的内容
        String code=ran.toString();
        session.setAttribute("code", code);
		IndustrySMS.execute(mobile,code);
		return "1";
	}
	//用来进行美颜社注册
	@RequestMapping("/register")
	@ResponseBody
	public String register(HttpSession session,HttpServletRequest request,@RequestParam("checkCode")String checkCode){
		Bis_custom_user user=(Bis_custom_user)session.getAttribute("customUser");
		System.out.println(user==null);
		String code1=(String) session.getAttribute("code");
		Map<String, Object> map =init(request);
		
		System.err.println(checkCode+"***************************************");
		System.err.println(code1+"***************************************");
		if(code1==checkCode || code1.equals(checkCode)){
			user.setPassword(map.get("password").toString());
			user.setMobile(map.get("mobile").toString());
			this.commonService.updateCustomUser(user);
			/*map.put("cdkey", map.get("password").toString());
			this.service.saveBis_coupon_record(map);*/
			return "1";
		}else {
			return "0";
		}
		
	}
	
	/**
	 * 用来进行验证用户是否已注册
	 * @param request
	 * @return
	 */
	@RequestMapping("/select")
	@ResponseBody
	public String select(HttpServletRequest request){
		String mobile=request.getParameter("mobile");
		Bis_custom_user list = bsus.getBis_custom_userByid(mobile);
		if (list!=null && !list.equals("")) {
			return "1";
		}else{
			return "0";
		}
		
	}
	
	
	//用来显示微信平台的登陆页面
	@RequestMapping("/wechatLogin")
	public String wechatLogin(){
		return "/backstage/cl/wechat/wechatLogin";
	}
	
	/**
	 * 将登录页面的参数封装
	 * @param request
	 * @return
	 */
	private Map init(HttpServletRequest request) {
		Map<String, Object> map =new HashMap<String, Object>();
		String mobile=request.getParameter("mobile");
		String password =request.getParameter("password");
		System.err.println(mobile+":::"+password+"------------------------------------------------------------");
		map.put("mobile", mobile);
		map.put("password",password);
		return map;
	}
	
	
	//用来显示微信平台的主页面
	@RequestMapping("/findProject")
	public String findProject(Bis_nurse_project bis_nurse_project,Model model,HttpServletRequest request,HttpSession session){
		//调用护理项目列表的显示
		List<Bis_nurse_project> list = impl.selectBis_nurse_project(bis_nurse_project);
		//将查询数据库返回的数据传入页面
		model.addAttribute("list", list);
		return "/backstage/cl/wechat/project/findProject";
	}
	
	//用来显示微信平台进入个人界面
	@RequestMapping("/Gohome")
	public String Gohome(Model model,HttpSession session){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		Map<String, Object> map=new HashMap<>();
		map.put("custom_user_id", ss.getCustom_user_id());
		Bis_custom_user bis_custom_user=bsus.getMobileBis_custom_user(map);
		model.addAttribute("bis_custom_user", bis_custom_user);
		return "/backstage/cl/wechat/my/home";
	}

	/**
	 * 找礼包页面
	 * @return
	 */
	//用来显示微信平台进入找礼包页面
	@RequestMapping("/GobagList")
	public String Gobag(){
		return "/backstage/cl/wechat/bag/bagList";
	}
	//礼包的详情以及包含的项目
	@RequestMapping("/GoshowBagDetail")
	public String GoshowBagDetail(Model model,@RequestParam("bag_id")String bag_id){
		double price;
		double a=0;
		double b;
		Bis_nurse_bag bis_nurse_bag=impl.getBis_nurse_bagByid(bag_id);
		List<Bis_bag_project> bis_bag_project=impl.selectBis_bag_projectById(bag_id);
		for (Bis_bag_project bb : bis_bag_project) {
			a=a+bb.getBis_nurse_project().getRmb_price();
		}
		b=bis_nurse_bag.getBag_total_price();
		price=a-b;
		model.addAttribute("price", price);
		model.addAttribute("a", a);
		model.addAttribute("bis_bag_project1", bis_bag_project);
		model.addAttribute("aaa", bis_nurse_bag);
		return "/backstage/cl/wechat/bag/showBagDetail";
	}
	
	
	/**
	 * 
	 * 用户协议的页面
	 * @return
	 */
	//用来显示用户协议的页面
	@RequestMapping("/GoserverDeal")
	public String GoserverDeal(){
		return "/backstage/cl/wechat/serverDeal";
	}	
	
	

}






